package c926.xiaoduyun.xiaodu.mservice;

import kd.bos.dataentity.entity.CloneUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.*;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.apache.commons.lang.StringUtils;


import java.util.EventObject;

/**
 * 单据界面插件
 */
public class XiaoduyunLevelToStepPlugin extends AbstractFormPlugin implements ClickListener, RowClickEventListener, Plugin, BeforeF7SelectListener {


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        EntryGrid eg = this.getView().getControl("c926_levelentity");
        EntryGrid subEntryGrid = this.getView().getControl("c926_stepentity");
        subEntryGrid.addRowClickListener(this);
        eg.addRowClickListener(this);
        BasedataEdit edit = this.getView().getControl("c926_xiaodustep");
        edit.addBeforeF7SelectListener(this);
        this.addItemClickListeners("tbmain");

    }

    /**
     * 拿到当前点击的消毒等级
     * 匹配对应的消毒步骤
     * 返回消毒步骤数据
     *
     * @param evt
     */
    @Override
    public void entryRowClick(RowClickEvent evt) {
        RowClickEventListener.super.entryRowClick(evt);
        Control control = (Control) evt.getSource();
        EntryGrid entryGrid = this.getView().getControl("c926_levelentity");
        int[] choice = entryGrid.getSelectRows();
        if (StringUtils.equals("c926_stepentity", control.getKey())) {
            if (choice.length == 0) {
                this.getView().showTipNotification("请先选择消毒等级");
            }
        }
        if (StringUtils.equals("c926_levelentity", control.getKey())) {
            this.getView().setEnable(choice.length != 0, 0, "c926_xiaodustep");
        }

    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        String fieldKey = beforeF7SelectEvent.getProperty().getName();
        if (StringUtils.equals(fieldKey, "c926_xiaodustep")) {
            int choice = this.getModel().getEntryCurrentRowIndex("c926_levelentity");
            DynamicObject levelEntity = this.getModel().getEntryRowEntity("c926_levelentity", choice);
            if (levelEntity != null) {
                Long id = levelEntity.getLong("c926_level.id");
                String number = levelEntity.getString("c926_level.number");
                QFilter qFilter1 = new QFilter("number", QCP.equals, number);
                QFilter qFilter2 = new QFilter("group.id", QCP.equals, id);
                ListShowParameter showParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
                showParameter.getListFilterParameter().getQFilters().add(qFilter2);
                showParameter.getTreeFilterParameter().getQFilters().add(qFilter1);
            }
        }
    }

    @Override
    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        String itemKey = evt.getItemKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if ("c926_publish".equals(itemKey)) {
            String status = dataEntity.getString("status");
            if (!"A".equals(status)) {
                this.getView().showTipNotification("最新版本的方案才能发布");
                evt.setCancel(true);
            }
        } else if ("bar_save".equals(itemKey)) {
            int[] selectLevelIndex = ((EntryGrid) this.getControl("c926_levelentity")).getSelectRows();
            int[] selectStepIndex = ((EntryGrid) this.getControl("c926_stepentity")).getSelectRows();
            if (selectLevelIndex.length == 0) {
                this.getView().showTipNotification("请选择消毒等级");
                evt.setCancel(true);
                return;
            }
            if (selectStepIndex.length == 0) {
                this.getView().showTipNotification("请选择消毒步骤");
                evt.setCancel(true);
            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String itemKey = evt.getItemKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if ("c926_publish".equals(itemKey)) {
            String number = dataEntity.getString("number");
            String entityName = "c926_xiaodurecord";
            QFilter qFilter = new QFilter("c926_scheme.number", QCP.equals, number);
            DynamicObject[] loads = BusinessDataServiceHelper.load(entityName, "id", new QFilter[]{qFilter});
            if (loads.length != 0) {
                dataEntity.set("status", "B");
                CloneUtils cloneUtils = new CloneUtils(false, true);
                DynamicObject old = (DynamicObject) cloneUtils.clone(dataEntity);
                old.set("status", "A");
                old.set("c926_version", old.getInt("c926_version") + 1);
                SaveServiceHelper.save(new DynamicObject[]{dataEntity, old});
            }
            this.getView().updateView();
            this.getView().showSuccessNotification("发布成功");
        }
    }
}